package com.oldwine.order.controller;

import com.oldwine.order.pojo.Order;
import com.oldwine.order.pojo.User;
import com.oldwine.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 请求路径:/order/101
     *
     * @param id 订单id
     * @return 订单
     */
    @GetMapping("/{orderId}")
    public Order queryOrderById(@PathVariable("orderId") Long id) {
        //1.查询订单信息
        Order order = orderService.queryOrderById(id);

        //远程调用用户微服务
        //2.根据查询到的userId查询用户信息
//        String url = "http://localhost:8081/user/" + order.getUserId();
        String url = "http://userService/user/" + order.getUserId();
        User user = restTemplate.getForObject(url, User.class);

        //3.将user对象填充入order对象中
        order.setUser(user);
        return order;
    }
}
